function NotePad_Data(data) {
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		try{
		info.Context=decodeURI(info.Context);
		}catch(e){}
		buffer.append('<div class="notepadlist border-bottom" id="'+info.ID+'"><div class="profile">'+info.Context+'</div><div class="date">'+info.CreateDate.ToMonth()+'</div></div>');
	});
	return buffer.toString();
};
function NotePadDetail_Data(id,context) {
	var buffer = new StringBuffer();
	buffer.append('<div class="notepaddetail"><textarea placeholder="输入记事内容">'+context.RecoverText()+'</textarea></div>');
	return buffer.toString();
};
function Open_Detail(id,context){
	OpenWindow(2,"记事本","保存",function(){
		var data = {};
			if(id==0)
			{
				data.act="Publish";
				data.model="notepad";
			}else{
				data.act = "Save_NotePad";
			}
			data.userid=userid;
			data.comid=comid;
			data.source=source;
			data.version=version;
			data.id=id;
			data.context=$("#contentlist2 textarea").val();
			if(data.context.Trim()=="")
			{
				plus.nativeUI.toast("请输入记事内容");
				return;
			}
			data.sign=GetSign(data.act);
			plus.nativeUI.showWaiting( "正在保存..." );
			CloseWaiting(20000);
			mui.post(index_domain, data, function(json) {
				plus.nativeUI.toast(json.msg);
				plus.nativeUI.closeWaiting();
				if(id==0)
				{
					Receive_NotePad(1);
					FindOAReport();
				}else
				{
					obj.find(".profile").html(data.context);
				}
				CloseWindow();
			},'json');
	});
	$("#contentlist2").html(NotePadDetail_Data(id,context));
};
function Init_NotePads(){
	get("divmsgname").addEventListener("tap",function(){
		OpenOAScreen(NotePad_Search_Code(),function(){
			Init_NotePad_Search();
		},function(){Receive_NotePad(1)});
	});
	
};


function NotePad_Search_Code(){
	var buffer = new StringBuffer();
	buffer.append('<div class="searchline"><div class="names">关键字</div><div class="option"><input id="keyword_search" type="text" placeholder="搜索关键字" /></div></div>');
	buffer.append('<div class="searchline"><div class="names">发布时间</div><div class="option"><input id="begindate_search" type="text" class="date" placeholder="开始时间" readonly="readonly"/><input id="enddate_search" type="text" class="date" placeholder="结束时间" readonly="readonly"/></div></div>');
	return buffer.toString();
};

function Init_NotePad_Search(){
	mui(".option").on("tap",".date",function(){
		obj=this;
			var t=new Date();
			if($(this).val()!="")
			{
				var year_month_day=$(this).val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]);
			}
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$(obj).val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				console.log( "未选择日期："+e.message );
			},{date:t});
	});

};
function Receive_NotePad(pageindex,mbegindate,menddate)
{
	var data = {};
			data.act = "Find_NotePad";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.ids="";
			data.pagesize=PAGE_SIZE20;
			data.begindate = RUndefined($("#begindate_search").val());if(mbegindate){data.begindate=mbegindate;}
			data.enddate = RUndefined($("#enddate_search").val());if(menddate){data.enddate=menddate;}
			data.keyword = RUndefined($("#keyword_search").val());
			data.sign=GetSign(data.act);
			mui.getJSON(index_domain, data, function(json) {
				if(json.data.list.length>0)
				{
					if(pageindex==1)
					{
						$("#contentlist1").html(NotePad_Data(json.data.list));
					}else{
						$("#contentlist1").append(NotePad_Data(json.data.list));
					}
					setTimeout(function(){
						Init_NotePad();
						myScroll1.refresh();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.list.length!=data.pagesize)
					{
						$("#loading1").html("");
					}else{
						$("#loading1").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					if(pageindex==1)
					{
						$("#contentlist1").html("");
						$("#loading1").html("主人，没有找到您需要的数据");
					}else
					{
						$("#loading1").html("没有更多了");
					}
				}
         		pageindex = parseInt($("#divmsg1").attr("pageindex"))+1;
         		$("#divmsg1").attr("pageindex",pageindex);
			});
};

function Init_NotePad(){
	mui("#contentlist1").off("tap",".notepadlist"); //必须有
	mui("#contentlist1").on("tap",".notepadlist",function() {
		obj=$(this);
		Open_Detail(obj.attr("id"),obj.find(".profile").html());
	});
	
	mui("#divmsg1").off("longtap",".notepadlist");
	mui("#divmsg1").on("longtap",".notepadlist",function(){
			obj=$(this);
        	plus.nativeUI.confirm("确定要删除吗？", function(event){
				if(event.index==1)
				{
					var data = {};
				    data.act = "Delete_NotePad";
				    data.ids = obj.attr("id");
				    data.userid=userid;
					data.comid=comid;
					data.source=source;
					data.sign=GetSign(data.act);
				    mui.getJSON(index_domain, data, function(json) {
				    	plus.nativeUI.toast(json.msg);
						if(json.state)
						{
							obj.hide(300);
							FindOAReport();
						}
				    });	
				}
			},"提示", ["取消","确认"]); 
	});
};